    /******************************************************
    ***              main.c: EXI decoder                ***
    ******************************************************/


#include "Processing.h"


#ifdef DEBUGMODE

unsigned char *GetRequest(char *filename, unsigned char *buffer, int *bufferlength)
{
    FILE *file;        // buffer for input file
    int filesize;
    int i = 0;

    file = fopen(filename, "rb"); // read only mode, binary
    if (file != NULL)
    {
        fseek(file, 0L, SEEK_END);   // jump to end of file to get file size
        filesize = ftell(file);
        *bufferlength = filesize;    // set buffer length

        printf("Input file size / Bytes: %d\n", filesize);

        fseek(file, 0L, SEEK_SET);  // jump to start of file
        // allocate memory for buffer
        buffer = (unsigned char *)malloc(sizeof(unsigned char)*filesize);
        for (i = 0; i < filesize; i++)
        {
            buffer[i] = getc(file);    // read EXI stream byte-wise
        }
        fclose(file);                  // close file buffer
    } else {

        printf("Error opening input file: %s\n", filename);

        return NULL;
    }

    return buffer;
}


int main(int argc, char *argv[])
{
    int ExitCode = 0;

    int bufferlength;
    char *Inputfilename;
    unsigned char *Inputbuffer = NULL;

    int i;

    if (argc < 2)
    {
        printf("Schema-informed EXI decoder demo\n");
        printf("Usage: program executable <input filename> [-Options]\n");
        printf("Options: -b Use Bit Alignment (Default: Byte Alignment)\n");

        ExitCode = 1;

    } else {
        Inputfilename = argv[1];

        printf("\n");
        printf("Using input file for request: %s\n", Inputfilename);

        for (i = 2; i < argc; i++)
        {
            switch(argv[i][1])
            {
                case 'b':
                	printf("Bit Alignment will be used!\n");
                    setAlignment(true);
                    break;
                default:
                	printf("Unknown Option, will be ignored: %s\n", argv[i]);
                    break;
            }
        }
        if (getAlignment() == false)
        {
        	printf("Byte Alignment will be used!\n");
        }

    }
    if (ExitCode == 0)
    {
        Inputbuffer = GetRequest(Inputfilename, Inputbuffer, &bufferlength);

        if (Inputbuffer == NULL)
        {
            return 1;
        }

//        Processing(Inputbuffer, &bufferlength);
        Processing(Inputbuffer);
    }

    getchar();

    return ExitCode;
}

#endif


#ifndef DEBUGMODE

int main()
{
	// fixed inputfile (10_strict_*.exi)

	// bit aligned
//	setAlignment(true);
//	unsigned char exi_file_bitaligned[] = {0x90, 0x15, 0x60, 0x03, 0x0A, 0x60, 0x54, 0x00, 0x02, 0x7A, 0xC0, 0x0C, 0x88, 0xA0, 0x0C, 0x95, 0x00, 0x31, 0x62, 0xF7, 0x57, 0x26, 0xE3, 0xA7, 0x57, 0x56, 0x96, 0x43, 0xA3, 0x73, 0x26, 0x66, 0x43, 0x16, 0x16, 0x43, 0x62, 0xD3, 0x23, 0x76, 0x63, 0x22, 0xD3, 0x43, 0x53, 0x43, 0x22, 0xD3, 0x86, 0x66, 0x23, 0x02, 0xD3, 0x03, 0x73, 0x53, 0x13, 0x03, 0x66, 0x56, 0x46, 0x16, 0x56, 0x63, 0x3C, 0x01, 0x14, 0x61, 0x63, 0x73, 0x3A, 0x41, 0x69, 0x72, 0x43, 0x6F, 0x6E, 0x64, 0x69, 0x74, 0x69, 0x6F, 0x6E, 0x65, 0x72, 0x94, 0x00, 0xE5, 0x86, 0x5A, 0x1D, 0x1D, 0x1C, 0x0E, 0x8B, 0xCB, 0xCC, 0x4C, 0x8D, 0xCB, 0x8C, 0x0B, 0x8C, 0x0B, 0x8C, 0x4E, 0x8D, 0x4D, 0x4D, 0x0D, 0x4E, 0x4B, 0xF8, 0x08, 0xEB, 0x0B, 0x1B, 0x99, 0xD2, 0x0B, 0x4B, 0x92, 0x1B, 0x7B, 0x73, 0x23, 0x4B, 0xA3, 0x4B, 0x7B, 0x73, 0x2B, 0x92, 0x4B, 0x73, 0xA3, 0x2B, 0x93, 0x33, 0x0B, 0x1B, 0x28, 0xB5, 0xA1, 0xD1, 0xD1, 0xC0, 0xE8, 0xBC, 0xBD, 0xDD ,0xDD, 0xDC, 0xB9, 0xDD, 0xCC, 0xD1, 0x90, 0xB9, 0xBD, 0xC9, 0x9C, 0xBD, 0x95, 0xE1, 0x85, 0xB5, 0xC1, 0xB1, 0x95, 0xCC, 0xBD, 0x05, 0xA5, 0xC9, 0x0D, 0xBD, 0xB9, 0x91, 0xA5, 0xD1, 0xA5, 0xBD, 0xB9, 0x95, 0xCB, 0x8C, 0x50, 0x07, 0x90, 0x80, 0x20, 0x46, 0x56, 0xE8, 0x5D, 0x55, 0xB9, 0xA5, 0xD9, 0x95, 0xC9, 0xCD, 0xA5, 0xD1, 0xE4, 0x81, 0xBD, 0x98, 0x81, 0x49, 0xBD, 0xCD, 0xD1, 0xBD, 0x8D, 0xAD, 0x0D, 0xB4, 0x3A, 0x3A, 0x38, 0x1D, 0x17, 0x97, 0xBB, 0xBB, 0xBB, 0x97, 0x3A, 0xB7, 0x34, 0x96, 0xB9, 0x37, 0xB9, 0xBA, 0x37, 0xB1, 0xB5, 0x97, 0x32, 0x32, 0x84, 0x01, 0x00, 0xD8, 0x61, 0x05, 0xA5, 0xC9, 0x0D, 0xBD, 0xB9, 0x91, 0xA5, 0xD1, 0xA5, 0xBD, 0xB9, 0x95, 0xC8, 0x81, 0x4D, 0x95, 0xC9, 0xD9, 0xA5, 0x8D, 0x94, 0x81, 0x4C, 0x4B, 0x8C, 0x00, 0x0B, 0xD2, 0x8A, 0x00, 0xE2, 0x10, 0x04, 0x02, 0xB0, 0x0C, 0x10, 0x6A, 0xB3, 0x2B, 0x93, 0x9B, 0x4B, 0x7B, 0x71, 0x01, 0x81, 0x71, 0x88, 0x26, 0x32, 0x33, 0x34, 0x64, 0x35, 0x65, 0x34, 0x66, 0x2D, 0x63, 0x32, 0x33, 0x66, 0x2D, 0x34, 0x64, 0x39, 0x31, 0x2D, 0x38, 0x34, 0x62, 0x61, 0x2D, 0x31, 0x66, 0x31, 0x35, 0x39, 0x32, 0x38, 0x34, 0x62, 0x33, 0x37, 0x61, 0xF0};
//	int bufferlength = 336;
//	unsigned char *Inputbuffer = exi_file_bitaligned;

	// byte aligned
//	setAlignment(false);
//	unsigned char exi_file_bytealigned[] = {0x90, 0x05, 0x01, 0x01, 0x08, 0x00, 0x18, 0x00, 0xA6, 0x05, 0x00, 0x08, 0x00, 0x00, 0x02, 0x07, 0x01, 0x01, 0x06, 0x00, 0x06, 0x01, 0x00, 0x00, 0x04, 0x05, 0x00, 0x19, 0x00, 0x01, 0x05, 0x00, 0x0C, 0x01, 0x01, 0x02, 0x2F, 0x75, 0x72, 0x6E, 0x3A, 0x75, 0x75, 0x69, 0x64, 0x3A, 0x37, 0x32, 0x66, 0x64, 0x31, 0x61, 0x64, 0x36, 0x2D, 0x32, 0x37, 0x66, 0x32, 0x2D, 0x34, 0x35, 0x34, 0x32, 0x2D, 0x38, 0x66, 0x62, 0x30, 0x2D, 0x30, 0x37, 0x35, 0x31, 0x30, 0x36, 0x65, 0x64, 0x61, 0x65, 0x66, 0x33, 0x03, 0x00, 0x01, 0x14, 0x61, 0x63, 0x73, 0x3A, 0x41, 0x69, 0x72, 0x43, 0x6F, 0x6E, 0x64, 0x69, 0x74, 0x69, 0x6F, 0x6E, 0x65, 0x72, 0x01, 0x00, 0x05, 0x00, 0x0E, 0x01, 0x01, 0x02, 0x19, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30, 0x2E, 0x31, 0x3A, 0x35, 0x35, 0x34, 0x35, 0x39, 0x2F, 0x03, 0x01, 0x01, 0x1D, 0x61, 0x63, 0x73, 0x3A, 0x41, 0x69, 0x72, 0x43, 0x6F, 0x6E, 0x64, 0x69, 0x74, 0x69, 0x6F, 0x6E, 0x65, 0x72, 0x49, 0x6E, 0x74, 0x65, 0x72, 0x66, 0x61, 0x63, 0x65, 0x00, 0x2D, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x77, 0x77, 0x77, 0x2E, 0x77, 0x73, 0x34, 0x64, 0x2E, 0x6F, 0x72, 0x67, 0x2F, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x73, 0x2F, 0x41, 0x69, 0x72, 0x43, 0x6F, 0x6E, 0x64, 0x69, 0x74, 0x69, 0x6F, 0x6E, 0x65, 0x72, 0x01, 0x01, 0x01, 0x00, 0x00, 0x01, 0x04, 0x05, 0x00, 0x1E, 0x01, 0x00, 0x02, 0x00, 0x02, 0x04, 0x65, 0x6E, 0x02, 0x17, 0x55, 0x6E, 0x69, 0x76, 0x65, 0x72, 0x73, 0x69, 0x74, 0x79, 0x20, 0x6F, 0x66, 0x20, 0x52, 0x6F, 0x73, 0x74, 0x6F, 0x63, 0x6B, 0x01, 0x00, 0x1B, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x77, 0x77, 0x77, 0x2E, 0x75, 0x6E, 0x69, 0x2D, 0x72, 0x6F, 0x73, 0x74, 0x6F, 0x63, 0x6B, 0x2E, 0x64, 0x65, 0x00, 0x02, 0x00, 0x02, 0x01, 0x05, 0x02, 0x18, 0x41, 0x69, 0x72, 0x43, 0x6F, 0x6E, 0x64, 0x69, 0x74, 0x69, 0x6F, 0x6E, 0x65, 0x72, 0x20, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x01, 0x00, 0x05, 0x31, 0x2E, 0x30, 0x00, 0x00, 0x01, 0x07, 0x02, 0x01, 0x00, 0x00, 0x02, 0x04, 0x05, 0x00, 0x1C, 0x01, 0x00, 0x02, 0x00, 0x02, 0x01, 0x05, 0x02, 0x01, 0x08, 0x01, 0x00, 0x0D, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x20, 0x30, 0x2E, 0x31, 0x00, 0x00, 0x26, 0x32, 0x33, 0x34, 0x64, 0x35, 0x65, 0x34, 0x66, 0x2D, 0x63, 0x32, 0x33, 0x66, 0x2D, 0x34, 0x64, 0x39, 0x31, 0x2D, 0x38, 0x34, 0x62, 0x61, 0x2D, 0x31, 0x66, 0x31, 0x35, 0x39, 0x32, 0x38, 0x34, 0x62, 0x33, 0x37, 0x61, 0x01, 0x01, 0x01, 0x01};
//	int bufferlength = 397;
//	unsigned char *Inputbuffer = exi_file_bytealigned;


	// empty (for code size testing -> decoder only)
	setAlignment(true);
	unsigned char *Inputbuffer = (unsigned char*)"";
	int bufferlength = 0;


//	Processing(Inputbuffer, &bufferlength);
	Processing(Inputbuffer);

	return 0;
}

#endif

